package utils;

import java.sql.*;

/**
 * @Author Su
 * @Date 2025/4/30 10:06
 * @Version 1.0 （版本号）
 */
// DBUtil.java - 数据库连接工具类
public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/cloud_doc?useSSL=false&serverTimezone=Asia/Shanghai";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }

    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                if (!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                System.err.println("关闭连接时出错: " + e.getMessage());
            }
        }
    }
}
